<?php

require_once 'helper/DbTable.php';

class Admin extends DbTable
{
    public static $_TABLE_NAME_ = 'yxb_admin';
    private $admin_id;
    private $admin_name;
    private $admin_password;
    public static $required = ['admin_name', 'admin_password'];
    public static $members_mapping = ['admin_id', 'admin_password', 'admin_name'];
    public static $relationship = [];

    public function __construct($name = null, $password = null)
    {
        if ($name && $password) {
            $this->admin_name = $name;
            $this->setAdminPassword($password,false);
        }
    }

    public static function getTableName()
    {
        return Admin::$_TABLE_NAME_;
    }

    public static function getRequired()
    {
        return Admin::$required;
    }

    public function add()
    {
        // TODO: Implement add() method.
        $format = "insert into %s(admin_name,admin_password) values('%s','%s')";
        $sql = sprintf($format, Admin::$_TABLE_NAME_, $this->admin_name, base64_encode($this->admin_password));
        return $sql;
    }

    public function delete()
    {
        // TODO: Implement delete() method.
        $format = "delete from %s where admin_id=%s;";
        $sql = sprintf($format, Admin::$_TABLE_NAME_, $this->admin_id);
        return $sql;
    }

    public function edit($data)
    {
        // TODO: Implement edit() method.
    }


    /**
     * @return mixed
     */
    public function getAdminId()
    {
        return $this->admin_id;
    }

    /**
     * @param mixed $admin_id
     */
    public function setAdminId($admin_id)
    {
        $this->admin_id = $admin_id;
    }

    /**
     * @return mixed
     */
    public function getAdminName()
    {
        return $this->admin_name;
    }

    /**
     * @param mixed $admin_name
     */
    public function setAdminName($admin_name)
    {
        $this->admin_name = $admin_name;
    }

    /**
     * @return string
     */
    public function getAdminPassword(): string
    {
        return $this->admin_password;
    }

    /**
     * @param string $admin_password
     * @param boolean $load
     */
    public function setAdminPassword(string $admin_password, $load = true)
    {
        if ($load) {
            $this->admin_password = base64_decode($admin_password);
            return;
        }
        $this->admin_password = $admin_password;
    }

    public static function query($condition)
    {
        // TODO: Implement query() method.

        if (isset($condition['name']) && isset($condition['value'])) {
            $format = "select * from %s where %s='%s';";
            $sql = sprintf($format, Admin::$_TABLE_NAME_, $condition['name'], $condition['value']);
            return $sql;
        }
        return null;
    }

    public function __toString()
    {
        return json_encode([
            "adminName" => $this->getAdminName(),
            "adminPassword" => $this->getAdminPassword(),
            "adminId" => $this->getAdminId()
        ]);
    }

    public function loadData($data)
    {
        foreach (Admin::$members_mapping as $member) {
            if (array_key_exists($member,$data)) {
                DbTable::callSetMethod($this, $member, $data[$member]);
            } else {
                return false;
            }
        }
        return true;
    }

    public function confirm($password)
    {
        error_log($this->getAdminPassword());
        return $this->getAdminPassword() == $password;

    }

    public function copy()
    {
        // TODO: Implement copy() method.
    }

    public function changeTo($obj)
    {
        // TODO: Implement changeTo() method.
    }
}